package com.servlet.form;

import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.listener.ContextListener;
import com.servlet.model.base.Area;
import com.servlet.model.base.Contact;
import com.servlet.model.base.Profile;
import com.servlet.model.base.Status;
import com.servlet.model.database.BaseAcces;

public class ProfilPersoForm extends HttpServlet
{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		boolean correct = false;
		
		HttpSession session = request.getSession();
		//BaseAcces base = (BaseAcces) this.config.getServletContext().getAttribute("base");
		
		String emailOriginal = (String) session.getAttribute("email");
		
		BaseAcces base = ContextListener.base;
		
		String email = request.getParameter("email");
		//il ne faut pas qu'on change l'email
		email = emailOriginal;
		String status = request.getParameter("status");
		String jour = request.getParameter("jour");
		String mois = request.getParameter("mois");
		String annee = request.getParameter("annee");
		
		String cityCode = request.getParameter("cityCode");
		String pays = request.getParameter("pays");
		String ville = request.getParameter("ville");
		
		String resultat = null;

		Date date = null;
		
		if( jour.compareTo("-1") == 0 || annee.compareTo("-1") == 0 || mois.compareTo("-1") == 0  )
		{
			date = null;
		}
		else
		{
			if(annee != "0" && mois != "0" && jour != "0")
			{
				int jourInt = Integer.parseInt(jour);
				if(jourInt <10 && jourInt > 0)
				{
					jour = "0"+jourInt;
				}
				int moisInt = Integer.parseInt(mois);
				if(moisInt <10 && moisInt > 0)
				{
					mois = "0"+moisInt;
				}
				date = Date.valueOf(annee + "-" + mois + "-" + jour);
			}
			
			date = Date.valueOf( annee + "-" + mois + "-" + jour);
		}
		
		if( email != null && email != "" && cityCode != null && cityCode != "" && pays != null && pays != "" && ville != null && ville != "" )
		{
			if( Format.verifyEmail(email) && Format.verifyName(pays) && Format.verifyName(ville) )
			{
				if( Format.verifyNumero(cityCode) )
				{
					if( !base.existEmail(email) || email.compareTo(emailOriginal) == 0  )
					{
						correct = true;
					}
					else
					{
						resultat = "email déjà utilisé";
					}
				}
				else
				{
					resultat = "code postal non valide";
				}
			}
			else
			{
				resultat = "email non valide";
			}
		}
		else
		{
			resultat = "vous devez remplir tous les champs";
		}
		
		if( correct )
		{
			System.out.println("Mise a jour du compte : " + email + " " + status + " " + date);

			Profile profile = ContextListener.base.findProfile(emailOriginal);
			
			profile.setEmail(email);
			profile.setStatus(new Status(Integer.parseInt(status)));
			
			if( !(profile.getBirth_date() != null && date == null ) )
					profile.setBirth_date(date);
			
			profile.setWorld_area( new Area(Integer.parseInt(cityCode) , pays , ville) );
			
			profile.setIs_modified_birth_date(true);
			profile.setIs_modified_status(true);
			profile.setIs_modified_world_area(true);
			
			base.updateProfil(profile);
			
			session.setAttribute("email", email );
			
			session.setAttribute("resultat", "Modification réussie !!");
			
			ArrayList<Contact> a = base.findContacts(emailOriginal);
			
			java.util.Date dd = Calendar.getInstance().getTime();
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String dat = dateFormat.format(dd);
			Date dateActuelle = Date.valueOf(dat);
			
			for( int i = 0 ; i < a.size() ; i++ )
			{
				if( a.get(i).getIs_accepted() )
				if( a.get(i).getEmail_user1().compareTo(email) == 0 )
					base.addNews( email , a.get(i).getEmail_user2() , dateActuelle , 0 );
				else
					base.addNews( email , a.get(i).getEmail_user1() , dateActuelle , 0 );
			}
			
			response.sendRedirect("profil");
		}
		else
		{
			System.out.println(resultat);
			
			session.setAttribute("resultat", "La modification a échoué : " + resultat );
			
			response.sendRedirect("profilPerso");
		}
	}
}

